
const app = getApp()

Page({
  data: {
    hiddenBox1: false,
    hiddenBox2: true,
    navBarFlag: 1,
    click_button: 0,
    phone: '',
    code: '',
    password: '',
    send: true,
    alreadySend: false,
    second: 60,
    checked: false,
    role: '出题专家'
  },
  
  onShow:function(){
    var that=this;
    // that.getOpenid();
    if(wx.getStorageSync('isLogin')==1){
      wx.switchTab({
        url: '/pages/center/index',
      })
    }
  },

  //tab切换
  tabClick: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      navBarFlag: index
    });
    if (index == 1) {
      that.setData({
        hiddenBox1: false,
        hiddenBox2: true,
      });
    } else if (index == 2) {
      that.setData({
        hiddenBox1: true,
        hiddenBox2: false,
      });
    }
  },

  checkedTap:function(e){
    this.setData({
      checked: !this.data.checked
    })
  },

  xieyi:function(e){
    wx.showToast({
      title: '《云数用户协议》',
      icon: 'none',
      duration: 2000,
    })
  },

  zhengce:function(e){
    wx.showToast({
      title: '《云数隐私政策》',
      icon: 'none',
      duration: 2000,
    })
  },

  submit_result:function(){
    var that = this;
    if (that.data.phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000,
      })
    } else {
      if(that.data.navBarFlag == 1){
        if (!(that.data.password)) {
          wx.showToast({
            title: '请输入密码',
            icon: 'none',
            duration: 2000,
          })
        }else if (that.data.checked == false) {
          wx.showToast({
            title: '请同意《云数用户协议》和《云数隐私政策》',
            icon: 'none',
            duration: 2000,
          })
        }else{
          that.lastLogin();
        }
      }else{
        if (!(that.data.code)) {
          wx.showToast({
            title: '请输入验证码',
            icon: 'none',
            duration: 2000,
          })
        }else if (that.data.checked == false) {
          wx.showToast({
            title: '请同意《云数用户协议》和《云数隐私政策》',
            icon: 'none',
            duration: 2000,
          })
        }else{
          that.lastLogin();
        }
      }
      
    }
  },

  lastLogin: function () {
    let that = this;
    that.setData({
      click_button: 1
    })
    let password = that.data.password 
    if(that.data.navBarFlag == 2){
      password = that.data.code
    }
    wx.request({
      url: app.globalData.domain + 'api/tea/login/',
      method: "POST",
      data: {
        login_type: 2,
        username: that.data.phone,
        code_id: 2,
        password: that.data.password,
        
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        if (res.data.code == 0) {
          app.setToken(res.data.token)
          app.setUserId(res.data.id)
          app.setRole(res.data.open_role)
          wx.setStorageSync("isLogin", 1);
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              that.setData({
                phone: '',
                code: '',
                password:'',
                click_button: 0,
                send: true,
                alreadySend: false,
                second: 60
              })
            }
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/center/index',
            })
          }, 1000);
        } else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000,
            success: function() {
              that.setData({
                code: '',
                password:'',
                click_button: 0,
                send: true,
                alreadySend: false,
                second: 60
              })
            }
          })
        }
      }
    })
  },

  //获取openid
  getOpenid: function () {
    var that = this;
    wx.login({
      success: function (myres) {
        wx.request({
          url: app.globalData.domain + 'index/getOpenid/',
          method: "POST",
          data: {
            code: myres.code,
          },
          header: {
            "Content-Type" : "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res.data.data.openid);
            wx.setStorageSync("openid", res.data.data.openid);
          }
        })
      }
    })
  },

  getPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  getCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  sendMsg: function (options) {
    var that=this;
    if(that.data.phone==""){
      wx.showToast({
        title: '请填写您的手机号码',
        icon: 'none',
        duration: 2000,
      })
    }else{
      if ((/^1(3|4|5|7|8|9)\d{9}$/.test(that.data.phone))) {
        that.setData({
          alreadySend: true,
          send: false
        })
        that.timer();
        wx.request({
          url: app.globalData.domain + 'api/sms/send/',
          method: "POST",
          data: {
            phone: that.data.phone,
            code_id: 2
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if(res.data.code == -1){
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: res.data.message,
                icon: 'success',
                duration: 2000
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '请填写正确的手机号码',
          icon: 'error',
          duration: 2000,
        })
      }
    }
  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  goRegister : function () {
    wx.navigateTo({
      // url: '/pages/center/register/index',
      url: '/pages/center/register/person',
    })
  }

})
